package club.xyes.zkh.ecommerce.wechat.dto;

import club.xyes.zkh.ecommerce.commons.entity.TransactionLog;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.annotation.JSONField;
import lombok.Data;

/**
 * Create by 郭文梁 2019/4/20 0020 10:44
 * TradeQueryResult
 * 交易查询接口返回数据格式
 *
 * @author 郭文梁
 * @data 2019/4/20 0020
 */
@Data
public class TradeQueryResult {
    public static final String RETURN_CODE_SUCCESS = "01";

    public static TradeQueryResult fromJsonString(String json) {
        TradeQueryResult res = JSON.parseObject(json, TradeQueryResult.class);
        res.setSourceJson(json);
        return res;
    }

    @JSONField(name = "return_code")
    private String returnCode;
    @JSONField(name = "return_msg")
    private String returnMsg;
    @JSONField(name = "key_sign")
    private String keySign;
    @JSONField(name = "result_code")
    private String resultCode;
    @JSONField(name = "pay_type")
    private String payType;
    @JSONField(name = "trade_state")
    private String tradeState;
    @JSONField(name = "merchant_name")
    private String merchantName;
    @JSONField(name = "merchant_no")
    private String merchantNo;
    @JSONField(name = "terminal_id")
    private String terminalId;
    @JSONField(name = "terminal_trace")
    private String terminalTrace;
    @JSONField(name = "terminal_time")
    private String terminalTime;
    @JSONField(name = "pay_trace")
    private String payTrace;
    @JSONField(name = "pay_time")
    private String payTime;
    @JSONField(name = "total_fee")
    private String totalFee;
    @JSONField(name = "end_time")
    private String endTime;
    @JSONField(name = "out_trade_no")
    private String outTradeNo;
    @JSONField(name = "channel_trade_no")
    private String channelTradeNo;
    @JSONField(name = "channel_order_no")
    private String channelOrderNo;
    @JSONField(name = "user_id")
    private String userId;
    private String attach;
    @JSONField(name = "receipt_fee")
    private String receiptFee;
    private String typeMsg;
    /**
     * 保存API返回的源数据
     */
    private String sourceJson;

    public boolean isSuccess() {
        return RETURN_CODE_SUCCESS.equals(returnCode);
    }

    /**
     * 获取与之匹配的交易记录状态码
     *
     * @return 状态码
     */
    public int getTransactionLogStatus() {
        int res;
        switch (resultCode) {
            case "01":
                res = TransactionLog.STATUS_PAID;
                break;
            case "02":
                res = TransactionLog.STATUS_CLOSED;
                break;
            case "03":
                res = TransactionLog.STATUS_PENDING_PAYMENT;
                break;
            default:
                throw new RuntimeException("Unexpected Api result code:" + resultCode);
        }
        return res;
    }
}
